import os

import joblib


class project_tools:
    insects_img_dirname="insects_imgs"
    processed_img_dirname="processed_imgs"
    characteristic_dirname="chas_data"
    models_dirname="trainning_models"
    insects_tags_dirname="insects_tags"
    trainning_source_dirname="trainning_data_source"
    model_name="Model4April.pkl"

    @staticmethod
    def project_root_path(project_name=None):
        """
        获取当前项目根路径
        :param project_name:
        :return: 根路径
        """
        PROJECT_NAME = 'insects_detection' if project_name is None else project_name
        project_path = os.path.abspath(os.path.dirname(__file__))
        root_path = project_path[:project_path.find(PROJECT_NAME) + len(PROJECT_NAME)]
        return root_path

    @staticmethod
    def prepare_working_dirs(project_root_dir):
        if not os.path.exists(project_root_dir+'\\'+project_tools.insects_img_dirname):
            os.mkdir(project_root_dir+'\\'+project_tools.insects_img_dirname)
        if not os.path.exists(project_root_dir+'\\'+project_tools.processed_img_dirname):
            os.mkdir(project_root_dir + '\\' + project_tools.processed_img_dirname)
        if not os.path.exists(project_root_dir + '\\' + project_tools.characteristic_dirname):
            os.mkdir(project_root_dir + '\\' + project_tools.characteristic_dirname)
        if not os.path.exists(project_root_dir + '\\' + project_tools.models_dirname):
            os.mkdir(project_root_dir + '\\' + project_tools.models_dirname)
        if not os.path.exists(project_root_dir + '\\' + project_tools.insects_tags_dirname):
            os.mkdir(project_root_dir + '\\' + project_tools.insects_tags_dirname)
        if not os.path.exists(project_root_dir + '\\' + project_tools.trainning_source_dirname):
            os.mkdir(project_root_dir + '\\' + project_tools.trainning_source_dirname)

    @staticmethod
    # 依据原图像存储的路径,在目标路径创建相同的父文件夹
    def prepare_img_output_dir(source_img_dir,target_img_dir):
        origin_folder_name=source_img_dir.rsplit('\\',1)[1]
        if not os.path.exists(target_img_dir + '\\' + origin_folder_name):
            os.mkdir(target_img_dir + '\\' + origin_folder_name)
        return target_img_dir+'\\'+origin_folder_name

    @staticmethod
    def save_trained_models(model,model_save_path,model_name):
        joblib.dump(model,model_save_path+model_name)
        print(model_name+"已写入")

